package com.autumn.platform.core.context.token;

import org.springframework.core.NamedInheritableThreadLocal;

import com.autumn.platform.core.Defaults;

public class TokenHolder {

    private static final ThreadLocal<Token> cache = new NamedInheritableThreadLocal<Token>("token");

    public static Token getToken(String extendedInformation) {
        Token token = cache.get();
        if (null == token) {
            token = getTokenService().allocateToken(extendedInformation);
            cache.set(token);
        }
        return token;
    }

    public static Token verifyToken(String key) {
        return getTokenService().verifyToken(key);
    }

    public static Token getToken() {
        return cache.get();
    }

    public static void setToken(Token token) {
        TokenHolder.cache.set(token);
    }

    public static void remove() {
        cache.remove();
    }

    private static TokenService getTokenService() {
        return Defaults.getDefaultComponent(TokenService.class);
    }
}
